package main.java.model;

import java.util.Observable;

public class HydraulicCircuit extends Observable {
    
	//static final variable Hin
    private static final int HIN = 10000;
    
    //static final variables to change positions id easier
    private static final int HIGH_ID = 0;
    private static final int INPROGRESS_ID = 1;
    private static final int DOWN_ID = 2;
    
    private int pressure;
    private int position;
    private boolean fullPression;
    
    /**
     * Constructor HydraulicCircuit
     */
    public HydraulicCircuit() {
        this.pressure = 0;
        this.position = HIGH_ID;
        this.fullPression = false;
    }

    /**
     * Set pressure value depending on the given element id
     * @param elementId : the element id, which hydro circuit
     * @param pressure : new pressure value
     */
    public void setPressure(int elementId, int pressure) {
        //which hydro circuit
        switch (elementId) { 
            //Close doors
            case 0: 
            //Retract gears
            case 2: 
                //If there is pressure
                reducePressionPhase(pressure);
                break;
            //Open doors
            case 1: 
            //Extend gears
            case 3:
                increasePressionPhase(pressure);
                break;
            default:
                break;
        }
    }

    /**
     * Setting pressure in order to close doors or to retract gears
     * @param pressure : new pressure value
     */
    public void reducePressionPhase(int pressure) {
        if (fullPression) { 
            //If pressure is zero
            if (this.pressure == 0) { 
                this.fullPression = false;
                //Set to high
                setPosition(HIGH_ID); 
            } else {
                //Decrease pressure
                if (this.pressure > pressure && this.pressure == (HIN - 1)) { 
                    //Set to in progress
                    setPosition(INPROGRESS_ID);
                }
                this.pressure = pressure;
            }
        }
    }
    
    /**
     * Setting pressure in order to open doors or to extend gears
     * @param pressure : new pressure value
     */
    public void increasePressionPhase(int pressure) {
        if (!fullPression) {
            //If max pressure 
            if (this.pressure == HIN) { 
                //Set to full
                this.fullPression = true; 
                //Set to down
                setPosition(DOWN_ID); 
            } else {
                //Increase pressure
                if (this.pressure < pressure && this.pressure == 1) { 
                    //Set to in progress
                    setPosition(INPROGRESS_ID); 
                }
                this.pressure = pressure;
            }
        }
    }
    
    /**
     * Return the position in a string
     * @return String : the position in a string
     */
    public String positionToString() {
        switch (position) {
            case HIGH_ID :
                return "High";
            case INPROGRESS_ID :
                return "InProgress";
            case DOWN_ID :
                return "Down";
            default :
                return "Default";
        }
    }
    
    /**
     * From the string value of the position
     * Get the number that matches
     * @param positionString : the string
     * @return int : the number
     */
    public int getPositionValue(String positionString) {
        switch (positionString) {
            case "High" :
                return HIGH_ID;
            case "InProgress" :
                return INPROGRESS_ID;
            case "Down" :
                return DOWN_ID;
            default :
                return -1;
        }
    }

    public int getPosition() {
        return position;
    }

    /**
     * Set Position and notify the main application
     * @param position : the new position
     */
    public void setPosition(int position) {
        this.position = position;
        
        //Setting changes
        setChanged(); 
        //Notify
        notifyObservers(positionToString()); 
    }

    public boolean isFullPression() {
        return fullPression;
    }

    public void setFullPression(boolean fullPression) {
        this.fullPression = fullPression;
    }
    
    public int getHin() {
        return HIN;
    }
    
    public int getPressure() {
        return pressure;
    }
}
